/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#include <QObject>
#include <QGraphicsItem>
#include <QGraphicsWidget>
#include <QGraphicsProxyWidget>
#include <QTimer>
#include "imagewidget.h"
#include "log.h"

ImageWidget::ImageWidget(QWidget* parent) : QFrame(parent),
	m_saved(false), m_clear(true), m_animated(false),
	m_imageID(0)
{
	m_movie = 0;
	m_view = new QGraphicsView(this);
	m_label = new QLabel();
	m_scene = new QGraphicsScene();
	m_item = m_scene->addWidget(m_label);
	m_view->setScene(m_scene);
	m_layout = new QStackedLayout(this);
	m_layout->addWidget(m_view);
	setLoadingImage();
}

ImageWidget::~ImageWidget()
{
	if(m_movie)
	{
		m_movie->stop();
		delete m_movie;
	}
	delete m_view;
	delete m_label;
	delete m_scene;
	delete m_layout;
}

void ImageWidget::setStaticImage(const QImage& image)
{
	m_clear = false;
	if(m_movie)
		delete m_movie;
	m_movie = 0;
	m_image = image;
	m_animated = false;
	if(m_movie)
	{
		delete m_movie;
		m_movie = 0;
	}
	QTimer::singleShot(0, this, SLOT(update()));
}

void ImageWidget::setAnimatedImage(const QByteArray& animimage)
{
	m_image = QImage();
	m_clear = false;
	m_animated = true;
	m_arr = animimage;
	m_buffer.setBuffer(&m_arr);
	m_buffer.open(QIODevice::ReadOnly);
	if(m_movie)
		delete m_movie;
	m_movie = new QMovie();
	connect(m_movie, SIGNAL(resized(QSize)), this, SLOT(animResized(QSize)));
	connect(m_movie, SIGNAL(frameChanged(int)), m_view->viewport(), SLOT(update()));
	m_movie->setDevice(&m_buffer);
	QTimer::singleShot(0, this, SLOT(update()));
}

void ImageWidget::setLoadingImage()
{
	m_clear = true;
	m_label->setText("No image loaded yet");
	m_view->centerOn(m_item);
}

void ImageWidget::update()
{
	m_label->setPixmap(QPixmap());
	m_label->setMovie(NULL);
	if(!m_animated)
	{
		m_label->setPixmap(QPixmap::fromImage(m_image));
		m_label->setText("");
		m_label->resize(m_label->pixmap()->size());
	}
	else
	{
		m_label->setMovie(m_movie);
		m_movie->start();
		m_label->setText("");
		QSize size = m_movie->currentImage().size();
		m_label->resize(size);
	}
	QGraphicsWidget* widget = static_cast<QGraphicsWidget*>(m_item);
	widget->resize(m_label->size());
	m_view->centerOn(m_item);
	widget->update();
	QFrame::update();
}

bool ImageWidget::isClear()
{
	return m_clear;
}

Wishmaster::ExtraContainer ImageWidget::extraContainer()
{
	return m_container;
}

void ImageWidget::setExtraContainer(Wishmaster::ExtraContainer container)
{
	m_container = container;
}

void ImageWidget::zoom()
{
	m_view->scale(1.25, 1.25);
}

void ImageWidget::unzoom()
{
	m_view->scale(0.8, 0.8);
}

QStringList ImageWidget::tags()
{
	return m_tags;
}

void ImageWidget::setTags(const QStringList& tags)
{
	m_tags = tags;
}

void ImageWidget::setSaved(bool s)
{
	m_saved = s;
}

bool ImageWidget::isSaved()
{
	return m_saved;
}

int ImageWidget::imageId()
{
	return m_imageID;
}

void ImageWidget::setImageId(int id)
{
	m_imageID = id;
}

QImage ImageWidget::image()
{
	return m_image;
}

bool ImageWidget::isAnimated()
{
	return m_animated;
}

QByteArray ImageWidget::array()
{
	return m_arr;
}

void ImageWidget::animResized(const QSize& size)
{
	m_label->resize(size);
	m_view->viewport()->update();
}

